package com.example.minio_demo.controller;

import com.example.minio_demo.service.MinioService;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author TonySong
 * @date 2025/8/9 0009
 * @time 17:44
 */
@RestController
@RequestMapping("/file")
public class UploadFileController {

    @Resource
    private MinioService minioService;

    @PostMapping("/upload")
    public ResponseEntity<String> upload(@RequestParam("file")MultipartFile file)  {
        try {
            String objName = this.minioService.upload(file);
            return ResponseEntity.ok("文件上传成功，文件名："+objName);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("文件上传失败！"+e.getMessage());
        }
    }

    @GetMapping("/getUrl")
    public ResponseEntity<String> getFileUrl(@RequestParam String objectName){

        try {
            String url = this.minioService.getFileUrl(objectName, 10);
            return ResponseEntity.ok(url);
        } catch (Exception e) {
            return ResponseEntity.status(500).body("获取文件地址失败！"+e.getMessage());
        }
    }

    @DeleteMapping("/remove")
    public ResponseEntity<String> removeFile(@RequestParam String objectName){{
        try {
            this.minioService.removeFile(objectName);
            return ResponseEntity.ok("文件删除成功！");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("文件删除失败！"+e.getMessage());
        }
    }
    }
}
